package com.loong.android.tools;

/**
 * 颜色工具类
 */
public class Color {

    /**
     * 取得RGBA颜色值
     * @param red 红色，取值范围：0-255
     * @param green 绿色，取值范围：0-255
     * @param blue 蓝色，取值范围：0-255
     * @param alpha 透明度，取值范围：0-255
     */
    public static int rgba( int red, int green, int blue, int alpha) {
        alpha = Math.max(Math.min(alpha, 255), 0);
        red = Math.max(Math.min(red, 255), 0);
        green = Math.max(Math.min(green, 255), 0);
        blue = Math.max(Math.min(blue, 255), 0);
        return android.graphics.Color.argb(alpha, red, green, blue);
    }

    /**
     * 取得RGB颜色值
     * @param red 红色，取值范围：0-255
     * @param green 绿色，取值范围：0-255
     * @param blue 蓝色，取值范围：0-255
     */
    public static int rgb(int red, int green, int blue) {
        red = Math.max(Math.min(red, 255), 0);
        green = Math.max(Math.min(green, 255), 0);
        blue = Math.max(Math.min(blue, 255), 0);
        return android.graphics.Color.rgb(red, green, blue);
    }

    /**
     * 文本转颜色值
     * @param colorStr 颜色文本
     */
    public static int parseColor(String colorStr) {
        colorStr = colorCheck(colorStr, "00000000");
        return android.graphics.Color.parseColor(colorStr);
    }

    private static String colorCheck(String colorStr, String defColor) {
        if (colorStr == null || colorStr.length() <= 0) return defColor;
        boolean hasStart = false;
        String fistStr = colorStr.substring(0, 1);
        switch (colorStr.length()) {
            default:
                return defColor;
            case 9: //#FFFFFFFF
            case 5: //#FFFFF
            case 7: //#FFFFFF
                if (!fistStr.equals("#")) return defColor;
                hasStart = true;
                break;
            case 8: //FFFFFFFF
            case 3: //FFF
            case 6: //FFFFFF
                if (fistStr.equals("#")) return defColor;
                break;
            case 4: //#FFF 或 FFFF
                hasStart = fistStr.equals("#");
                break;
        }
        if (hasStart) colorStr = colorStr.substring(1);

        char[] tmpc = colorStr.toCharArray();
        for (char aTmpc : tmpc) {
            if (aTmpc < 48 || (aTmpc > 57 && aTmpc < 65) || (aTmpc > 70 && aTmpc < 97) || aTmpc > 102) {
                return defColor;
            }
        }
        char[] tmpc2 = new char[8];
        int index = 0;
        switch (tmpc.length) {
            case 8:
                return "#" + colorStr;
            case 6:
                return "#FF" + colorStr;
            case 4:
                for (char aTmpc : tmpc) {
                    tmpc2[index] = aTmpc;
                    index++;
                    tmpc2[index] = aTmpc;
                    index++;
                }
                return "#" + String.valueOf(tmpc2);
            case 3:
                tmpc2[0] = 'F';
                tmpc2[1] = 'F';
                index = 2;
                for (char aTmpc : tmpc) {
                    tmpc2[index] = aTmpc;
                    index++;
                    tmpc2[index] = aTmpc;
                    index++;
                }
                return "#" + String.valueOf(tmpc2);
        }
        return defColor;
    }

    //颜色-透明
    public final static int TRANSPARENT = 0;
    //颜色-白色
    public final static int WHITE = 0xFFFFFFFF;
    //颜色-黑色
    public final static int BLACK = 0xFF000000;
    //颜色-灰色
    public final static int GRAY = 0xFF808080;
    //颜色-深灰
    public final static int DARK_GRAY = 0xFFA9A9A9;
    //颜色-浅灰色
    public final static int LIGHT_GRAY = 0xFFD3D3D3;
    //颜色-绿色
    public final static int GREEN = 0xFF008000;
    //颜色-深绿色
    public final static int DARK_GREEN = 0xFF006400;
    //颜色-浅绿色
    public final static int LIGHT_GREEN = 0xFF90EE90;
    //颜色-绿黄色
    public final static int GREEN_YELLOW = 0xFFADFF2F;
    //颜色-青色
    public final static int CYAN = 0xFF00FFFF;
    //颜色-深青色
    public final static int DARK_CYAN = 0xFF008B8B;
    //颜色-浅青色
    public final static int LIGHT_CYAN = 0xFFE0FFFF;
    //颜色-蓝色
    public final static int BLUE = 0xFF0000FF;
    //颜色-深蓝色
    public final static int DARK_BLUE = 0xFF00008B;
    //颜色-浅蓝色
    public final static int LIGHT_BLUE = 0xFFADD8E6;
    //颜色-天空蓝
    public final static int SKY_BLUE = 0xFF87CEEB;
    //颜色-黄色
    public final static int YELLOW = 0xFFFFFF00;
    //颜色-浅黄色
    public final static int LIGHT_YELLOW = 0xFFFFFFE0;
    //颜色-黄绿色
    public final static int YELLOW_GREEN = 0xFF9ACD32;
    //颜色-米黄色
    public final static int BEIGE = 0xFFF5F5DC;
    //颜色-金色
    public final static int GOLD = 0xFFFFD700;
    //颜色-棕色
    public final static int BROWN = 0xFFA52A2A;
    //颜色-红色
    public final static int RED = 0xFFFF0000;
    //颜色-深红色
    public final static int DARK_RED = 0xFF8B0000;
    //颜色-绯红色
    public final static int CRIMSON = 0xFFDC143C;
    //颜色-褐红色
    public final static int MAROON = 0xFF800000;
    //颜色-品红
    public final static int MAGENTA = 0xFFFF00FF;
    //颜色-粉红色
    public final static int PINK = 0xFFFFC0CB;
    //颜色-浅粉红色
    public final static int LIGHT_PINK = 0xFFFFB6C1;
    //颜色-深粉红色
    public final static int DARK_PINK = 0xFFFF1493;
    //颜色-橙色
    public final static int ORANGE = 0xFFFFA500;
    //颜色-深橙色
    public final static int DARK_ORANGE = 0xFFFF8C00;
    //颜色-橙红色
    public final static int ORANGE_RED = 0xFFFF4500;
    //颜色-紫色
    public final static int VIOLET = 0xFFEE82EE;
    //颜色-深紫色
    public final static int DARK_VIOLET = 0xFF9400D3;
    //颜色-紫红色
    public final static int VIOLET_RED = 0xFFD02090;
}
